package ch13;

import java.util.ArrayList;

import java.util.Random;
// NewCustomer.java
// Represents the NewCustomer option

public class NewCustomer extends Option {

	// irrelevant stringarray for menu-visualization
	private ArrayList<String> stringArray = new ArrayList<String>();
	private Keypad keypad;

	// NewCustomer constructor
	public NewCustomer(int userID, Screen atmScreen,
			BankDatabase atmBankDatabase, Keypad atmKeypad) {
		super(userID, atmScreen, atmBankDatabase);

		keypad = atmKeypad;
	} // end NewCustomer constructor

	// creates a new customer in the ATM
	public void execute() {
		BankDatabase bankDatabase = getBankDatabase();
		Screen screen = getScreen();
		Random rn = new Random(); // random method for the customer pin
		int x = 1;
		int customerID;
		ArrayList<Customer> customers = bankDatabase.getCustomerList(); // henter
																		// arraylist
																		// af
																		// customer
																		// og
																		// gemmer
																		// under
																		// customers
		
		// initialize random customerID
		customerID = rn.nextInt(99999);
	
		while (x == 1) {
			for (Customer currentCustomer : customers) {
				// return current customer if match found
				if (currentCustomer.getCustomerID() == customerID) {
					customerID = rn.nextInt(99999);
					x = 1;
				} 
				else	// exit loop
				{ 
					x = 2;
				}	// end else
			}	// end for
		}	// end while
		
		// initialize random pin
		int pin = rn.nextInt(99999);

		stringArray.clear();
		stringArray.add("Enter  customers CPR: ");
		screen.setKasse(stringArray, 10);
		long CPR = keypad.getInputL();

		stringArray.clear();
		stringArray.add("Enter customers first name: ");
		screen.setKasse(stringArray, 10);
		String firstName = keypad.getInputS();
		
		stringArray.clear();
		stringArray.add("Enter customers last name:  ");
		screen.setKasse(stringArray, 10);
		String lastName = keypad.getInputS();
		
		// use the addCustomers method to add a customer to the ATM
		bankDatabase.addCustomers(new Customer(customerID, pin, CPR, firstName, lastName));

		stringArray.clear();
		stringArray.add("A new customer has been added ");
		stringArray.add("First Name: " + firstName);
		stringArray.add("Last Name: " + lastName);
		stringArray.add("CPR: " + CPR);
		stringArray.add("CustomerID:  " + customerID);
		stringArray.add("Pin: " + pin);
		screen.setKasse(stringArray, 10);
	}
} // end class NewCustomer